import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import {
  getRoleList,
  getRoleById,
  createRole,
  updateRole,
  deleteRole,
  assignPermissions,
} from '@/api/role'

const initialState = {
  roleList: [],
  currentRole: null,
  loading: false,
  error: null,
}

// 获取角色列表
export const fetchRoleList = createAsyncThunk(
  'role/fetchRoleList',
  async (_, { rejectWithValue }) => {
    try {
      const data = await getRoleList()
      return data
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 获取角色详情
export const fetchRoleById = createAsyncThunk(
  'role/fetchRoleById',
  async (id, { rejectWithValue }) => {
    try {
      const data = await getRoleById(id)
      return data
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 创建角色
export const addRole = createAsyncThunk(
  'role/addRole',
  async (data, { rejectWithValue }) => {
    try {
      const result = await createRole(data)
      return result
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 更新角色
export const modifyRole = createAsyncThunk(
  'role/modifyRole',
  async ({ id, data }, { rejectWithValue }) => {
    try {
      const result = await updateRole(id, data)
      return result
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 删除角色
export const removeRole = createAsyncThunk(
  'role/removeRole',
  async (id, { rejectWithValue }) => {
    try {
      const data = await deleteRole(id)
      return { id, data }
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

// 分配权限
export const assignRolePermissions = createAsyncThunk(
  'role/assignRolePermissions',
  async ({ id, permissionIds }, { rejectWithValue }) => {
    try {
      const data = await assignPermissions(id, permissionIds)
      return data
    } catch (error) {
      return rejectWithValue(error.message)
    }
  }
)

const roleSlice = createSlice({
  name: 'role',
  initialState,
  reducers: {
    clearError: (state) => {
      state.error = null
    },
    clearCurrentRole: (state) => {
      state.currentRole = null
    },
  },
  extraReducers: (builder) => {
    builder
      // 获取角色列表
      .addCase(fetchRoleList.pending, (state) => {
        state.loading = true
        state.error = null
      })
      .addCase(fetchRoleList.fulfilled, (state, action) => {
        state.loading = false
        state.roleList = action.payload.data || action.payload
      })
      .addCase(fetchRoleList.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
      // 获取角色详情
      .addCase(fetchRoleById.pending, (state) => {
        state.loading = true
      })
      .addCase(fetchRoleById.fulfilled, (state, action) => {
        state.loading = false
        state.currentRole = action.payload.data || action.payload
      })
      .addCase(fetchRoleById.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
      // 创建角色
      .addCase(addRole.pending, (state) => {
        state.loading = true
      })
      .addCase(addRole.fulfilled, (state) => {
        state.loading = false
      })
      .addCase(addRole.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
      // 更新角色
      .addCase(modifyRole.pending, (state) => {
        state.loading = true
      })
      .addCase(modifyRole.fulfilled, (state) => {
        state.loading = false
      })
      .addCase(modifyRole.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
      // 删除角色
      .addCase(removeRole.pending, (state) => {
        state.loading = true
      })
      .addCase(removeRole.fulfilled, (state, action) => {
        state.loading = false
        state.roleList = state.roleList.filter((item) => item.id !== action.payload.id)
      })
      .addCase(removeRole.rejected, (state, action) => {
        state.loading = false
        state.error = action.payload
      })
  },
})

export const { clearError, clearCurrentRole } = roleSlice.actions
export default roleSlice.reducer

